/**
 * Copyright (C), 2015-2018
 * FileName: FilterConfiguration
 * Author:   Dayu Hou
 * Date:     2018/1/10 11:38
 * Description:
 * History:
 * <author>          <time>         <desc>
 */
package com.apache.tomcat;

import com.apache.client.servlet.RandCodeImageServlet;
import com.apache.ius.common.annotion.AnnotationUtil;
import com.apache.ius.listener.IusParamsListener;
import com.apache.passport.common.JwtClientFilter;
import com.apache.portal.common.restfull.ResultFullFilter;
import com.apache.portal.contorller.UploadServlet;
import com.apache.rpc.StartSocketPortListener;
import com.apache.rpc.common.RpcUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.filter.LicenseFilter;
import com.apache.uct.common.filter.UctClientFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.EventListener;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Dayu Hou
 * @since 1.0.0
 */
@Configuration
public class FilterConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

    @Bean
    public StartSocketPortListener startSocketPortListener() {
        String socketPort = StrUtil.doNull(env.getProperty("rpc.service.port"), "20808");
        StartSocketPortListener listener = new StartSocketPortListener(socketPort);
        if (StrUtil.isNotNull(env.getProperty("rpc.version"))) {
            RpcUtil.getInstance().setVersion(env.getProperty("rpc.version"));
        }
        //自动扫描包(拦截器专用)
        AnnotationUtil.getInstance().loadCalssBean("com.apache.ius");
        return listener;
    }

    /**
     * 加载listener
     */
    @Bean
    public ServletListenerRegistrationBean<EventListener> getDemoListener() {
        ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<>();
        registrationBean.setListener(new IusParamsListener());
        registrationBean.setOrder(2);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean encodingFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CharacterEncodingFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("encoding", "UTF-8");
        registration.addInitParameter("forceEncoding", "true");
        registration.setName("encodingFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean defaultBaseSecurityFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LicenseFilter());
        registration.addUrlPatterns("/*");
        registration.setName("licenseFilter");
        registration.setOrder(2);
        return registration;
    }

    @Bean
    public FilterRegistrationBean ssoClientFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new JwtClientFilter());
        registration.addUrlPatterns("/*", "/logout", "/ius");
        registration.setName("ssoClientFilter");
        registration.addInitParameter("sysCode", "ius");//指定白名单
        registration.setOrder(2);
        return registration;
    }

    @Bean
    public FilterRegistrationBean uctClientFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new UctClientFilter());
        registration.addUrlPatterns("/*");
        registration.setName("uctClientFilter");
        registration.addInitParameter("sysCode", "ius");//指定白名单
        registration.setOrder(3);
        return registration;
    }

    @Bean
    public FilterRegistrationBean resultFullFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new ResultFullFilter());
        registration.addUrlPatterns("/common/coding/*","/trans/template/*","/res/uct/*");//多个英文逗号隔开
        registration.setName("resultFullFilter");
        registration.addInitParameter("servletPackage", "com.apache.portal");
        registration.setOrder(3);
        return registration;
    }

    @Bean
    public ServletRegistrationBean getServletRegistrationBean() {  //一定要返回ServletRegistrationBean
        ServletRegistrationBean bean = new ServletRegistrationBean(new UploadServlet());     //放入自己的Servlet对象实例
        bean.addUrlMappings("/uploadServlet");  //访问路径值
        return bean;
    }

    //注入文件验证码servlet
    @Bean
    public ServletRegistrationBean getRandCodeImageServlet() {  //一定要返回ServletRegistrationBean
        ServletRegistrationBean bean = new ServletRegistrationBean(new RandCodeImageServlet(), "/web/CheckRandCode");
        return bean;
    }
}